package cn.yzgy.yzgyoa.ucenter.controller;


import cn.yzgy.yzgyoa.common.annotation.ApiDesc;
import cn.yzgy.yzgyoa.common.crsp.StatusCode;
import cn.yzgy.yzgyoa.common.crsp.Urmf;
import cn.yzgy.yzgyoa.common.utils.ToolsUtil;
import cn.yzgy.yzgyoa.ucenter.entity.SysOrganization;
import cn.yzgy.yzgyoa.ucenter.entity.SysUser;
import cn.yzgy.yzgyoa.ucenter.service.ISysOrganizationService;
import cn.yzgy.yzgyoa.ucenter.service.ISysUserDetailService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import cn.yzgy.yzgyoa.common.controller.BaseController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 组织机构表
支持公司多级结构 前端控制器
 * </p>
 *
 * @author Yqsen
 * @since 2020-03-26
 */
@RestController
@RequestMapping("/ucenter/sysOrganization")
public class SysOrganizationController extends BaseController {
    private final ToolsUtil toolsUtil;
    private final ISysUserDetailService iSysUserDetailService;
    private final ISysOrganizationService iSysOrganizationService;

    public SysOrganizationController(ToolsUtil toolsUtil, ISysUserDetailService iSysUserDetailService, ISysOrganizationService iSysOrganizationService) {
        this.toolsUtil = toolsUtil;
        this.iSysUserDetailService = iSysUserDetailService;
        this.iSysOrganizationService = iSysOrganizationService;
    }

    /**
     * 获取组织机构数据列表
     * @return 成功状态
     */
    @ApiDesc("获取组织机构数据列表")
    @GetMapping("/queryAllOrganization")
    @RequiresPermissions("api:ucenter:sysOrganization:get")
    public Object queryAllOrganization(){
        //组装Mybatis-Plus 分页查询结构，将条件放入QueryWarpper
        IPage<SysOrganization> ipage = new Page<>(Integer.parseInt("0"),Integer.parseInt("2000"));
        QueryWrapper<SysOrganization> wrapper = new QueryWrapper<>();

        Object organizationIpage=iSysOrganizationService.queryAll(ipage,wrapper);
        return Urmf.success("获取Organization数据列表", StatusCode.SUCCESS,organizationIpage);
    }

    /**
     * 根据前端传入对象修改数据表内容
     * @param sysOrganization 组织机构对象
     * @return 成功状态
     */
    @ApiDesc("修改组织机构数据")
    @PutMapping("/changeInfo")
    @RequiresPermissions("api:ucenter:sysOrganization:changeInfo")
    public Object changeInfo(@RequestBody  SysOrganization sysOrganization){

        int num=iSysOrganizationService.changeInfo(sysOrganization);
        if (num <= 1) {
            return Urmf.fail("修改Organization数据内容", StatusCode.FAILED, "修改数据失败！");
        }
        return Urmf.success("修改Organization数据内容", StatusCode.SUCCESS,num);
    }

    /**
     * 根据主键删除数据
     * @param sysOrganization 主键
     * @return 成功或失败状态
     */
    @ApiDesc("删除组织机构数据")
    @DeleteMapping("/del")
    @RequiresPermissions("api:ucenter:sysOrganization:del")
    public Object del(@RequestBody Map<String, String> sysOrganization){
        int num = iSysOrganizationService.delData(sysOrganization.get("oid"));
        if (num <= 0) {
            return Urmf.fail("改变Organization数据状态", StatusCode.FAILED, "级联删除数据失败！");
        }
        return Urmf.success("改变Organization数据状态", StatusCode.SUCCESS,"级联删除数据成功！");
    }

    /**
     * 新增表数据
     * @param sysOrganization 组织机构对象
     * @return 成功或失败状态
     */
    @ApiDesc("新增组织机构数据")
    @PostMapping("/add")
    @RequiresPermissions("api:ucenter:sysOrganization:add")
    public  Object addOrganization(@RequestBody SysOrganization sysOrganization){
        if (iSysOrganizationService.check(sysOrganization) == 1) {
            return Urmf.success("新增Organization数据", StatusCode.SUCCESS, "已存在该数据！");
        }
        String dataPerm = iSysOrganizationService.getParentEnAbbr(sysOrganization);
        dataPerm = dataPerm+"-"+toolsUtil.getDataPerm();
        sysOrganization.setEnAbbr(dataPerm);
        sysOrganization.setDataPerm(dataPerm);
        String id = iSysOrganizationService.getNewid(sysOrganization);
        if (id == null) {
            id = sysOrganization.getParentId() + "01";
        }
        sysOrganization.setId(Long.parseLong(id));
        int num=iSysOrganizationService.addSysOrganization(sysOrganization);
        if (num <= 0) {
            return Urmf.success("新增Organization数据", StatusCode.SUCCESS, "数据添加失败！");
        }
        return Urmf.success("新增Organization数据", StatusCode.SUCCESS,"数据添加成功！");
    }


    /**
     * 为前端下拉树提供data数据
     * @return 下拉json数据
     */
    @ApiDesc("组织机构数据树状数据查询")
    @RequestMapping("/getOrganizationlist")
    @RequiresPermissions("api:ucenter:sysOrganization:getOrganizationlist")
    public  String getOrganizationlist(){
        JSONObject organizationJson = toolsUtil.getReturnData("Organization");

        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String enAbbr = iSysUserDetailService.dataPerm(user.getId());
        List<SysOrganization> organizations = iSysOrganizationService.getSysOrganizationList(enAbbr);
        for (SysOrganization organization : organizations) {
            organization.setCheckArr("0");
            organization.setTitle(organization.getName());
        }
        organizations.get(0).setParentId(Long.parseLong("0"));
        organizationJson.put("data",organizations);
        System.out.println(organizationJson.toJSONString());
        return organizationJson.toJSONString();
    }
}
